//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var mallUrl = require("url/mall");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");

//UI
var headerAout = ui("headerAout");
var params={};

var hotWord = [{"name":"葡萄酒","selected":0,"bgColor":"00000000","val":8},{"name":"毛巾","selected":0,"bgColor":"00000000","val":10},{"name":"香米","selected":0,"bgColor":"00000000","val":11},
                 {"name":"阿胶","selected":0,"bgColor":"00000000","val":21}, {"name":"除螨","selected":0,"bgColor":"00000000","val":20}]; 
var hotWordGrids = mm("do_ListData");
var do_GridView_1 = ui("do_GridView_1");
hotWordGrids.addData(hotWord);
do_GridView_1.bindItems(hotWordGrids);
do_GridView_1.refreshItems();
//头部
var addheader = headerAout.add("header", "source://view/reuse/searchHeader.ui", 0, 0);
var textFieldSearch=ui(addheader+".textFieldSearch")
textFieldSearch.setFocus(true);

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"searchHint" : "搜索商品"
});
header.refreshData();

headerLeft.on("touch", function() {
	do_App.closePage()
});

getLstByCategory();
function getLstByCategory(){
	var data = do_Page.getData();
	if(data && data.categoryId){
		textFieldSearch.setFocus(false);
		do_Page.hideKeyboard();
		params.categoryId = data.categoryId;
		loadData();
	}
}

var search;
textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});
var sendBtn = ui(addheader + ".sendBtn");
sendBtn.on("touch", function(data, e) {
	sendSearch();
});
function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		loadData();
	}else{
		nf.toast("请输入商品关键字");
	}
}

function loadData(){
	params.name = search;
	helperOpen.open("source://view/mall/search/mallSearch.ui",params);
}

//订阅GridView的touch点击事件
do_GridView_1.on("touch", function(data) {
	var data_temp = hotWordGrids.getRange(0);
	search = data_temp[data].name;
	loadData();
});